import { _decorator, Component, Node, AssetManager } from 'cc';
import { GameCore } from '../../Base/Class';
import { NewClass } from '../../Base/Global';
import { AssetInfo } from '../ResourceLoad/base/AssetInfo';
import { ResourceLoadSpace } from '../ResourceLoad/_h/Index';
import { UIData } from './base/UIData';
import { UIComponentCreateFactor } from './UIComponentCreateFactor';
const { ccclass, property } = _decorator;

@ccclass('UIBase')
export abstract class UIBase extends GameCore.ClassBase  {
    public uiNode : Node = null;
    public uiParentNode : Node = null;
    protected preloadMap : Map<number, any> = new Map<number, any>();
    private _uiComponentFactor : UIComponentCreateFactor = null;
    public GetUIComponentFactor(buildIfNotExits : boolean) : UIComponentCreateFactor
    {
        if(this._uiComponentFactor == null && buildIfNotExits)
        {
            this._uiComponentFactor = NewClass(UIComponentCreateFactor);
        }
        return this._uiComponentFactor;
    }
    
    private collection : ResourceLoadSpace.IAssetCollectionInfo = null;
    public GetAssetBundle(assetInfo : AssetInfo) : AssetManager.Bundle
    {
        if(this.collection != null)
        {
            return this.collection.GetAsset(assetInfo);
        }   
        return null;
    }
    public SetAssetCollection(collection : ResourceLoadSpace.IAssetCollectionInfo)
    {
        this.collection = collection;
    }

    public SetParent(parent: Node)
    {
        this.uiParentNode = parent;
        if(this.uiNode != null) this.uiNode.setParent(this.uiParentNode);
    }
    public Init(uiNode: Node)
    {
        this.uiNode = uiNode;
        if(this.uiParentNode != null) this.uiNode.setParent(this.uiParentNode);
    }

    public SetData(uiData : UIData){}

    // 注意 AssetInfo 只返回值要唯一的，最好是返回一个静态只读的类。 内部采用instanceId去做对比
    public abstract GetAssetInfo() : AssetInfo;
    public GetPreloadAssetInfos() : AssetInfo[] {return null;}

    public abstract OnLoadUIMap();
    public abstract OnLoad();
    public abstract OnDestroy();
    public RecycleAsset()
    {
        // 这个collection 不是自己生成的，所有销毁要放在生成的地方去管理。
        // 这里只是将obj引用给清理掉
        this.collection = null;
    }

    // 便捷销毁接口-uibase调用的是manager， component调用的是另外一个
    public abstract OnDestroySelf();
}


